import Vue from "vue";
import Vuex from "vuex";
import chatStore from './chatStore.js';
import friendStore from './friendStore.js';
import userStore from './userStore.js';
import groupStore from './groupStore.js';
// import modules from "./modules";
// import getters from "./getters";
//导入插件
import createPersistedState from 'vuex-persistedstate'


Vue.use(Vuex);

const modulesFiles = require.context("./modules", true, /\.js$/);
const modules = modulesFiles.keys().reduce((modules, modulePath) => {
	// set './app.js' => 'app'
	const moduleName = modulePath.replace(/^\.\/(.*)\.\w+$/, "$1");
	const value = modulesFiles(modulePath);
	modules[moduleName] = value.default;
	return modules;
}, {});
// import {
// 	createStore
// } from 'vuex';
// const store = createStore({
// 	modules: {
// 		chatStore,
// 		friendStore,
// 		userStore,
// 		groupStore
// 	},
// 	state: {},
// 	actions: {
// 		load(context) {
// 			return this.dispatch("loadUser").then(() => {
// 				const promises = [];
// 				promises.push(this.dispatch("loadFriend"));
// 				promises.push(this.dispatch("loadGroup"));
// 				promises.push(this.dispatch("loadChat"));
// 				return Promise.all(promises);
// 			})
// 		},
// 		unload(context) {
// 			context.commit("clear");
// 		}
// 	},
// 	strict: true
// })
export default new Vuex.Store({
	modules,
	//配置插件
	plugins: [
		createPersistedState({
			storage: {
				getItem: (key) => uni.getStorageSync(key),
				setItem: (key, value) => uni.setStorageSync(key, value),
				removeItem: (key) => uni.removeStorageSync(key),
			},
		}),
	]

});